package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.XinWenDaoImpl;
import com.ysd.dao.impl.XinWenLieBieDaoImpl;
import com.ysd.entity.XinWenLieBie;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminNewsFenLeiServlet extends BaseServlet {
	/**
	 * 查询所有新闻资讯
	 * localhost:8080/qiuxieshangcheng/adminNewsFenLei?reqName=addNewsFenLei
	 */
	
	public void getNewsFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		XinWenLieBieDaoImpl xwDao = new XinWenLieBieDaoImpl();
		List<XinWenLieBie> xwList=  xwDao.getXinWenLieBieList();
		
		// 发送Ajax请求，服务器基本会返回json格式字符串
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		
		String res = gson.toJson(xwList);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	public void delNewsFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int delxwId = Integer.parseInt(request.getParameter("deletexwId"));
		XinWenLieBieDaoImpl xw1Dao = new XinWenLieBieDaoImpl();
		int delnews=  xw1Dao.deleteXinWenFenLeiId(delxwId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		if (delnews>0) {//删除成功
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	
public void addNewsFenLei(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		XinWenLieBieDaoImpl xwDao = new XinWenLieBieDaoImpl();
		String xinWenFebLieName = request.getParameter("xinWenFebLieName");
		
		if (xinWenFebLieName!=null&&!xinWenFebLieName.equals("")) {
			List<XinWenLieBie> xwList=	xwDao.getXWByName(xinWenFebLieName);
			if (xwList.size()==0) {
				XinWenLieBie xw = new XinWenLieBie(0, xinWenFebLieName, 0);
				
				
				int n=  xwDao.addXinWenFenLei(xw);
				if (n > 0) {// 删除成功
					out.print(Result.toClient(true, "添加成功", ""));
				} else {// 删除失败
					out.print(Result.toClient(false, "添加失败", ""));
				}
			}else {
				out.print(Result.toClient(false, "已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
		
		
	}


/**
 * 回显数据
 * localhost:8080/qiuxieshangcheng/adminNewsFenLei?reqName=getNewById
 */

public void getNewById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("UTF-8");
	PrintWriter out = response.getWriter();
	
	XinWenLieBieDaoImpl xwDao = new XinWenLieBieDaoImpl();
	int Id = Integer.parseInt(request.getParameter("xId"));
	XinWenLieBie  xw = xwDao.getXWFLById(Id);
	
	
	Gson gson = new Gson();
	
	String res = gson.toJson(xw);
	out.print(res);
	
	out.flush();
	out.close();
}

/**
 * 修改新闻分类
 * localhost:8080/qiuxieshangcheng/adminNewsFenLei?reqName=updateXWCate
 */

public void updateXWCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("UTF-8");
	PrintWriter out = response.getWriter();
	
	XinWenLieBieDaoImpl xwDao = new XinWenLieBieDaoImpl();
	
	int xinWenFenLieId = Integer.parseInt(request.getParameter("xwCateId"));
	String xinWenFebLieName = request.getParameter("xwCateName");
	
	
	XinWenLieBie xw = new XinWenLieBie(xinWenFenLieId, xinWenFebLieName, 0);
	int n=   xwDao.updateXWXL(xw);
	if (n>0) {
		out.print(Result.toClient(true, "修改成功",""));
	} else {
		out.print(Result.toClient(false, "修改失败",""));
	}
	
	
	
	
	
	
	out.flush();
	out.close();
}




















}
